﻿// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"


struct Complex
{
	double r;
	double i;
};

void iteration(UINT32* colors, UINT32* buf, int width, Complex a, int k, int nx, int ny);


extern "C"
{
	void DoJulia(UINT32* colors, UINT32* buf, int width, int height)
	{
		const double x_min = -1.5, y_min = -1.5;
		const double x_max = 1.5, y_max = 1.5;

		double dx, dy = 0;
		dx = (x_max - x_min) / (width - 1);
		dy = (y_max - y_min) / (height - 1);
		for (int nx = 0; nx < width; nx++)
		{
			for (int ny = 0; ny < height; ny++)
			{
				int k = 0;
				Complex x0;
				x0.r = x_min + nx * dx;
				x0.i = y_min + ny * dy;
				iteration(colors, buf, width, x0, k, nx, ny);
			}
		}

	}

}


void iteration(UINT32* colors, UINT32* buf, int width, Complex a, int k, int nx, int ny)
{
	const int K = 255;
	const int M = 100;
	const double p = 0.33, q = 0.4;//常数C 
	double tmp = a.r * a.r + a.i * a.i;
	if (tmp > M)
	{
		buf[ny * width + nx] = colors[k];
		return;
	}
	if (k == K)
	{
		buf[ny * width + nx] = colors[0];
		return;
	}
	if (tmp <= M && k < K)
	{
		Complex b;
		b.r = a.r * a.r - a.i * a.i + p;
		b.i = 2 * a.r * a.i + q;
		k++;
		iteration(colors, buf, width, b, k, nx, ny);
	}
}